<?php

namespace App\Http\Controllers\User;

use App\Models\Index\LiveVideo;
use App\Models\User\LiveUser;
use App\Models\User\LiveUserCard;
use App\Models\User\LiveUserCollect;
use App\Models\User\LiveUserFollow;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
    public function personalPage(Request $request)
    {
        $visit_user_id = (int)$request->input('visit_user_id');
        if (empty($visit_user_id)) {
            return self::response(null,-1,'用户不存在哦~');
        }
        $user = LiveUser::getUserByUserId($visit_user_id);
        if (empty($user)) {
            return self::response(null,-2,'用户不存在哦~');
        }
        unset($user->user_password);
        unset($user->user_token);
        return self::response($user,'200');
    }

    public function videoList(Request $request)
    {
        $visit_user_id = (int)$request->input('visit_user_id');
        if (empty($visit_user_id)) {
            return self::response(null,-1,'用户不存在哦~');
        }
        $data = LiveVideo::getVideoListByUserId($visit_user_id);
        return self::response($data,'200');
    }

    public function superiorPhone(Request $request)
    {
        $invite_user_id = (int)$request->input('invite_user_id');
        if (empty($invite_user_id)) {
            return self::response(null,-1,'您的上级用户不存在哦~');
        }
        $phone = LiveUser::getPhoneByUserId($invite_user_id);
        return self::response(['phone' => $phone],'200');
    }

    public function modifySign(Request $request)
    {
        $user_id    = (int)$request->input('user_id');
        $sign       = trim($request->input('sign',''));
        if (empty($user_id)) {
            return self::response(null,-1,'用户id不能为空');
        }
        if (empty($sign)) {
            return self::response(null,-2,'签名不能为空');
        }
        if (mb_strlen($sign,'utf-8') > 16) {
            return self::response(null,-3,'签名不能为空');
        }
        $flag = LiveUser::modifySign($user_id,$sign);
        if (empty($flag)) {
            return self::response(null,-4,'签名位数不能超过16位哦~');
        }
        return self::response(null,'200');
    }

    public function modifyPhone(Request $request)
    {
        $user_id    = (int)$request->input('user_id');
        $phone      = trim($request->input('phone',''));
        if (empty($user_id)) {
            return self::response(null,-1,'用户id不能为空');
        }
        if (empty($phone)) {
            return self::response(null,-2,'联系方式不能为空');
        }
        if (mb_strlen($phone,'utf-8') > 50) {
            return self::response(null,-3,'联系方式位数不能超过50位哦~');
        }
        $flag = LiveUser::modifyPhone($user_id,$phone);
        if (empty($flag)) {
            return self::response(null,-4,'系统繁忙');
        }
        return self::response(null,'200');
    }

    public function collectList(Request $request)
    {
        $user_id    = (int)$request->input('user_id');
        $page       = (int)$request->input('page');
        if (empty($user_id)) {
            return self::response(null,-1,'用户id不能为空');
        }
        if (empty($page)) {
            return self::response(null,-2,'页码不能为空');
        }
        $data       = LiveUserCollect::getMovieByUserId($user_id);
        return self::response($data,'200');
    }

    public function followList(Request $request)
    {
        $user_id    = (int)$request->input('user_id');
        $page       = (int)$request->input('page');
        if (empty($user_id)) {
            return self::response(null,-1,'用户id不能为空');
        }
        if (empty($page)) {
            return self::response(null,-2,'页码不能为空');
        }
        $data       = LiveUserFollow::getFollowByUserId($user_id);
        return self::response($data,'200');
    }

    public function renew(Request $request)
    {
        $user_id    = (int)$request->input('user_id');
        $code       = trim($request->input('code',''));
        if (empty($user_id)) {
            return self::response(null,-1,'用户不存在哦~');
        }
        if (empty($code)) {
            return self::response(null,-2,'卡密不能为空哦~');
        }
        if (mb_strlen($code,'utf-8') != 36) {
            return self::response(null,-3,'卡密格式不正确哦~');
        }
        $card = LiveUserCard::getCardByCode($code);
        if (empty($card)) {
            return self::response(null,-4,'卡密不存在哦~');
        }
        if ($card->status == 1) {
            return self::response(null,-5,'卡密已被激活了哦~');
        }
        $flag1 = LiveUserCard::renewCard($code,$user_id);
        if (!$flag1) {
            return self::response(null,-6,'系统繁忙，请稍后再试哦~');
        }
        $user = LiveUser::getUserByUserId($user_id);
        if (empty($user)) {
            return self::response(null,-7,'用户不存在哦~');
        }
        $vip_dead_line = Carbon::parse($user->vip_dead_line)->timestamp;
        if ($vip_dead_line > time()) {
            $time = $vip_dead_line+$card->vip_time*3600*24;
        } else {
            $time = time()+$card->vip_time*3600*24;
        }
        if ($time > 4294967295) {
            $time = 4294967295;
        }
        $flag2 = LiveUser::addVipTime($user_id,$time);
        if (!$flag2) {
            return self::response(null,-8,'系统繁忙，请稍后再试哦~');
        }
        return self::response(['vip_dead_line' => date('Y-m-d',$time)],'200');
    }

    public function upload(Request $request)
    {
        $user_id    = (int)$request->input('user_id');
        $title      = (string)trim($request->input('title',''));
        $attribute  = (int)$request->input('attribute',1);
        $img        = $request->file('img');
        $video      = $request->file('video');
        $time       = (int)$request->input('time',0);
        if (empty($user_id)) {
            return self::response(null,-1,'用户不存在哦~');
        }
        if (!$request->hasFile('img')) {
            return self::response(null,-2,'请上传视频的缩略图哦~');
        }
        if (!$img->isValid()) {
            return self::response(null,-3,'缩略图上传出错了哦~');
        }
        if (!$request->hasFile('video')) {
            return self::response(null,-4,'请选择要上传的视频哦~');
        }
        if (!$video->isValid()) {
            return self::response(null,-5,'视频上传出错了哦~');
        }
        if (empty($time)) {
            return self::response(null,-6,'视频时长为空哦~');
        }
        $img_type   = $img->getMimeType();
        if (str_before($img_type,'/') !== 'image') {
            return self::response(null,-7,'请选择图片进行上传哦~');
        }
        $img_size   = $img->getSize();
        if ($img_size > 10*1024*1024) {
            return self::response(null,-8,'您的缩略图大小不可以超过10M哦~');
        }
        $video_type = $video->getMimeType();
        if (str_before($video_type,'/') !== 'video') {
            return self::response(null,-9,'请选择视频进行上传哦~');
        }
        $video_size = $video->getSize();
        if ($video_size > 100*1024*1024) {
            return self::response(null,-10,'您的视频大小不可以超过100M哦~');
        }
        $img_path   = $img->store('upload/img','public');
        $img_url    = asset('storage/'.$img_path);
        $video_path = $video->store('upload/video','public');
        $video_url  = asset('storage/'.$video_path);
        $flag   = LiveVideo::insert([
                    'user_id'       => $user_id,
                    'title'         => $title,
                    'img'           => $img_url,
                    'url'           => $video_url,
                    'type'          => $video_type,
                    'attribute'     => $attribute,
                    'time'          => $time,
                    'created_time'  => time(),
                ]);
        if (!$flag) {
            return self::response(null,-11,'上传视频失败，请稍后再试哦~');
        }
        return self::response(null,'200');
    }
}
